package net.akkz.action.question;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.akkz.beanFactory.BeanFactory;
import net.akkz.model.Question;
import net.akkz.model.Student;
import net.akkz.server.QuestionServer;

public class QuestionFind extends HttpServlet
{
	private QuestionServer questionServer;
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
	{
		int start = Integer.parseInt(request.getParameter("start"));
		int end = Integer.parseInt(request.getParameter("end"));
		int kind = (Integer.parseInt(request.getParameter("kind")));
		
		String skind = request.getParameter("kind");
		
		ArrayList<Question> result;
		
		if(skind.equals("all_time"))
		{
			result = questionServer.findQuestion(start, end);
		}
		else if(skind.equals("all_good"))
		{
			result = questionServer.findQuestionOrderByGoodNum(start, end);
		}
		else if(skind.endsWith("time"))
		{
			result = questionServer.findQuestionWithKind(start, end, kind);
		}
		else
		{
			result = questionServer.findQuestionOrderByGoodNumWithKind(start, end, kind);
		}
		
		request.setAttribute("questions", result);
	}
	
	@Override
	public void init()
	{
		BeanFactory  bf = (BeanFactory) this.getServletContext().getAttribute("beanFactory");
		
		questionServer = bf.getBean("questionServer");
	}
}
